<?php
class Orderin extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="orderin/index_view";
        $this->_data["titlePage"]="Quản lý phiếu nhập";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->load->model("morderin");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/orderin/index/",
            "total_rows"=>$this->morderin->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->morderin->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->morderin->listAll($config["per_page"], $start);
        
        //Show cart temp
        $this->load->library("cart");
        $this->_data["cart_content"]=$this->cart->contents();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function search() {
        $this->_data["loadPage"]="orderin/search_view";
        $this->_data["titlePage"]="Tìm kiếm phiếu nhập";
        
        $provider=$this->session->userdata("sess_search_provider");
        $datefrom=$this->session->userdata("sess_search_datefrom");
        $dateto=$this->session->userdata("sess_search_dateto");
        $pricefrom=$this->session->userdata("sess_search_pricefrom");
        $priceto=$this->session->userdata("sess_search_priceto");
        
        $this->load->model("mprovider");
        $this->load->helper("provider_helper");
        $this->_data["info_provider"]=$this->mprovider->listAll();
        
        $this->load->model("morderin");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/orderin/search/",
            "total_rows"=>$this->morderin->countAllResults($provider, $datefrom, $dateto, $pricefrom, $priceto)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->morderin->search($provider, $datefrom, $dateto, $pricefrom, $priceto, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->morderin->search($provider, $datefrom, $dateto, $pricefrom, $priceto, $config["per_page"], $start);
        
        //Show cart temp
        $this->load->library("cart");
        $this->_data["cart_content"]=$this->cart->contents();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchOrderin"])) {
            $sess=array(
                "sess_search_provider"=>$this->input->post("slProvider"),
                "sess_search_datefrom"=>$this->input->post("txtDateFrom"),
                "sess_search_dateto"=>$this->input->post("txtDateTo"),
                "sess_search_pricefrom"=>$this->input->post("txtFrom"),
                "sess_search_priceto"=>$this->input->post("txtTo")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/orderin/search");
        }
    }
    
    public function edit() {
        $this->_data["loadPage"]="orderin/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa phiếu nhập";
        
        $oi_id=$this->uri->segment(4);
        $this->load->model("morderin");
        if($this->morderin->getById($oi_id)!=false)
            $this->_data["info_edit"]=$this->morderin->getById($oi_id);
        $data=array(
            "oi_note"=>$this->input->post("txtNote")
        );
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtNote",
                "label"=>"Ghi Chú",
                "rules"=>""
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $this->morderin->update($data, $oi_id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa phiếu nhập thành công.");
            redirect(base_url()."admin_combo/orderin/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("morderin");
        $this->morderin->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa phiếu nhập thành công.");
        redirect(base_url()."admin_combo/orderin/");
    }
}
?>
